﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_ProductManagement : System.Web.UI.Page
{
    public int selectedCat = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadProduct();
            loadCategory();
        }
    }
    public void loadProduct()
    {
        ChoDienTuDataContext cdt = new ChoDienTuDataContext();
        int? count = 0;
        grvProduct.DataSource = cdt.Select_Count_Product(ref count);
        grvProduct.DataBind();
        lblNumrecord.Text = count.ToString();
    }
    public void loadCategory()
    {
        ChoDienTuDataContext cdt = new ChoDienTuDataContext();
        ddlcategory.DataSource = cdt.Select_AllCategory();
        ddlcategory.DataTextField = "categoryname";
        ddlcategory.DataValueField = "id";
        if (selectedCat != 0)
        {
            
            ddlcategory.DataTextField = "categoryname";
            ddlcategory.DataValueField = ""+selectedCat;
        }
        ddlcategory.DataBind();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        clean();
    }
    public void clean()
    {
        txtDescription.Text = "";
        txtID.Text = "";
        //txtImg.Text = "";
        txtName.Text = "";
        txtPrice.Text = "";
        txtQuantity.Text = "";
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        String name = txtName.Text;
        String img = txtImg.Text;
        String bigimg = txtBigImage.Text;
        String des = "Intel Core i7 720QM 4 * 1.6 GHz. 6MB, 8Thread 8192 MB (2Gx2 +4G) DDRAM3 1000GB hard disk drive SATA";
        int catId = int.Parse(ddlcategory.SelectedValue.ToString());
        int quantity = int.Parse(txtQuantity.Text);
        float price = float.Parse(txtPrice.Text);

        try
        {
            ChoDienTuDataContext cdt = new ChoDienTuDataContext();
            cdt.Insert_Product(catId, name, price, quantity, img,bigimg, des);
            cdt.SubmitChanges();
            loadProduct();
            clean();
        }
        catch
        {
        }

    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        int Id = int.Parse(txtID.Text);
        String name = txtName.Text;
        String img = txtImg.Text;
        String des = txtDescription.Text;
        String bigimg = txtBigImage.Text;
        //int catId = int.Parse(ddlcategory.SelectedValue.ToString());
        int quantity = int.Parse(txtQuantity.Text);
        float price = float.Parse(txtPrice.Text);

        if (txtID.Text == "")
            return;
        try
        {
            ChoDienTuDataContext cdt = new ChoDienTuDataContext();
            cdt.Update_Product(Id, name, price, quantity, img,bigimg, des);
            cdt.SubmitChanges();
            loadProduct();
            clean();
        }
        catch
        {
        }
    }
    protected void grvProduct_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = grvProduct.SelectedRow;
    }
    protected void grvProduct_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        GridViewRow row = grvProduct.Rows[e.NewSelectedIndex];
        int id = int.Parse(row.Cells[1].Text);
        int catid = int.Parse(row.Cells[4].Text);
        String name = row.Cells[5].Text;
        txtID.Text = id.ToString();
        txtName.Text = name;
        txtPrice.Text = row.Cells[6].Text;
        txtQuantity.Text = row.Cells[7].Text;
        //txtImg.Text = row.Cells[8].Text;
        txtDescription.Text = row.Cells[9].Text;
    }
    protected void grvProduct_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(grvProduct.Rows[e.RowIndex].Cells[1].Text);
        ChoDienTuDataContext cdt = new ChoDienTuDataContext();
        cdt.Delete_Product(id);
        loadProduct();
        grvProduct.EditIndex = -1;
    }
    protected void grvProduct_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ChoDienTuDataContext cdt = new ChoDienTuDataContext();

        grvProduct.DataSource = cdt.Select_AllProduct();
        grvProduct.PageIndex = e.NewPageIndex; 
        grvProduct.DataBind();
    }
}
